/** 
 * Description:商品评价类
 * Date:2012-6-10
 * Author:Along	
*/
package com.community.shopping.web;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.community.shopping.domain.GoodsComment;
import com.community.shopping.service.logic.GoodsCommentManager;
import com.community.shopping.service.logic.Page;



@Controller 
public class GoodsCommentControl {

	@Autowired  
    private GoodsCommentManager goodsCommentManager;
	/**
	 * 查询商品评价
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/goodscomment/query")
	public void queryComment(HttpServletRequest request,HttpServletResponse response)throws Exception
	{
		String goodsid=request.getParameter("goodsid");
		Page page=new Page(request,4);	
		List<GoodsComment> goodscomments=goodsCommentManager.queryOrderComment(goodsid, page);
		StringBuilder sb=new StringBuilder("{\"comments\":[");
		for(GoodsComment comment:goodscomments)
		{
			/*String username=comment.getUser().getUserName();
			username=username.substring(0, 3)+"****"+username.substring(7);
			String address="["+comment.getUser().getAddress().substring(0,4)+"]";*/
			if(sb.length()>20)
			{
				sb.append(",");	
			}
			sb.append("{");
			sb.append("\"id\":\""+comment.getId()+"\",");
			sb.append("\"score\":\""+comment.getScore()+"\",");
			sb.append("\"content\":\""+comment.getContent()+"\",");
			sb.append("\"createtime\":\""+comment.getCreateTime()+"\",");
			sb.append("\"username\":\""+comment.getUserName()+"\"");
			sb.append("}");		
		}
		sb.append("],\"pageno\":\""+page.getCurrentPage()+"\",\"totalpage\":\""+page.getTotalPages()
				+"\",\"totalsize\":\""+page.getTotalRowsAmount()+"\"}");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().print(sb.toString()); 	
	}
	
	/**
	 * 保存商品评价
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/goodscomment/save")
	public void saveComment(HttpServletRequest request,HttpServletResponse response)throws Exception
	{
		String goodsid=request.getParameter("goodsid");	
   	    String user=request.getParameter("user"); 
   	    user=java.net.URLDecoder.decode(user, "UTF-8");  
   	    String time=request.getParameter("time");
   	    String comment=request.getParameter("comment"); 
		comment=java.net.URLDecoder.decode(comment, "UTF-8");  
		String score=request.getParameter("score");
		int totalcomment=goodsCommentManager.saveComment(goodsid, user, time, score, comment);
		response.setCharacterEncoding("UTF-8");
		response.getWriter().print(totalcomment); 	
   	   
	}
}
